<template>
  <div class="login">
    <span> 欢迎登录</span>
    <div class="email input">
      <!-- <span>请输入邮箱</span> -->
      <input
        type="text"
        class="inputEmail"
        placeholder="请输入邮箱"
        v-model="email"
      />
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-youjian"></use>
      </svg>
    </div>
    <div class="pw input">
      <!-- <span>请输入密码</span> -->
      <input
        type="password"
        class="inputPw"
        placeholder="请输入密码"
        v-model="password"
        @keydown.enter="loginEvent"
      />
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-mima"></use>
      </svg>
    </div>
    <div class="btn">
      <button class="loginBtn" @click="register">注册</button>
      <button
        class="registerBtn"
        @click="loginEvent"
        @keydown.enter="loginEvent"
      >
        登录
      </button>
    </div>
    <div class="tishi" v-if="ifLogin">邮箱不存在或密码错误</div>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      email: "",
      password: "",
      ifLogin: false,
    };
  },
  methods: {
    loginEvent: async function () {
      let result = await this.$store.dispatch("loginUser", {
        email: this.email,
        password: this.password,
      });
      // console.log(result);
      if (result.data.state == "OK") {
        this.$router.push("/user");
      } else if (result.data.state == "ERROR") {
        // console.log('错误');
        
        this.password = "";
        this.ifLogin = true;
        if (this.ifLogin) {
          let btn = document.querySelector(".btn");
          btn.style.marginBottom = "0";
        } else {
          let btn = document.querySelector(".btn");
          btn.style.marginBottom = "1.7rem";
        }
      }
    },
    register: function () {
      this.$router.push("/register");
    },
  },
};
</script>

<style lang="less">
.login {
  min-width: 7.5rem;
  min-height: 100vh;
  background-color: #f5f6fa;
  display: flex;
  flex-direction: column;
  align-items: center;
  span {
    //   width: 2.4rem;
    letter-spacing: 0.1rem;
    padding-left: 0.1rem;
    text-align: justify;
    margin-top: 3rem;
    margin-bottom: 0.8rem;
    font-weight: bold;
    color: #192a56;
  }
  .input {
    display: flex;
    flex-direction: column;
    position: relative;
    margin-top: 0.4rem;
    margin-bottom: 0.15rem;
    // align-items: center;
    span {
      font-weight: normal;
      letter-spacing: 0rem;
      padding-left: 0.35rem;
      font-size: 0.4rem;
    }
    input {
      width: 5rem;
      height: 1rem;
      border: none;
      background-color: #f5f6fa;
      outline: none;
      border: solid 0.04rem #273c75;
      border-radius: 0.5rem;
      padding-left: 0.4rem;
      padding-right: 1rem;
      overflow: hidden;
      font-size: 0.4rem;
      color: #273c75;
    }
    input::-webkit-input-placeholder {
      color: #7f8fa6;
    }
    .icon {
      width: 0.4rem;
      height: 0.4rem;
      fill: #273c75;
      position: absolute;
      right: 0.4rem;
      top: 0.3rem;
    }
  }
  .btn {
    width: 5rem;
    height: 1rem;
    display: flex;
    justify-content: space-between;
    margin-top: 0.8rem;
    padding: 0 0.1rem;
    margin-bottom: 1.7rem;
    button {
      border: none;
      width: 2rem;
      border-radius: 0.5rem;
      color: white;
      font-size: 0.4rem;
      outline: none;
      &:first-child {
        background-color: #7688ba;
      }
      &:last-child {
        background-color: #5d76ba;
      }
    }
  }
  .tishi {
    margin-top: 0.4rem;
    color: #bf3030;
    font-size: 0.35rem;
    // display: none;
    margin-bottom: 1.7rem;
  }
}
</style>